﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BinhSon.Services.Contracts;
using BinhSon.Services.Impl;

namespace BinhSon.Presentation
{
    public partial class TongKetDiem : UserControl
    {
        private IBinhSonServices _bsServices; 

        public TongKetDiem()
        {
            InitializeComponent();
        }

        private void TongKetDiem_Load(object sender, EventArgs e)
        {
            this._bsServices = new BinhSonServices();
            this.LoadHocKy();
            this.LoadKhoi();            
        }

        private void LoadHocKy()
        {
            var result = this._bsServices.ListHocKy().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                hocKyComboBox.Items.Add(comboBoxModel);
            }
        }

        private void LoadKhoi()
        {
            var result = this._bsServices.ListKhoi().Select(x => new ComboBoxModel
            {
                Key = x.LookupReference,
                Value = x.Description
            });
            foreach (var comboBoxModel in result)
            {
                khoiComboBox.Items.Add(comboBoxModel);
            }
        }

        private void LoadMonHoc(ComboBoxModel selectedKhoi, ComboBox targetComboBox)
        {
            var result = this._bsServices.ListMonHoc(selectedKhoi.Key).Select(x => new ComboBoxModel
            {
                Key = x.MonHocId,
                Value = x.TenMonHoc
            });
            targetComboBox.Items.Clear();
            targetComboBox.Text = "";
            foreach (var comboBoxModel in result)
            {
                targetComboBox.Items.Add(comboBoxModel);
            }
        }

        private void khoiComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedKhoi = (ComboBoxModel)khoiComboBox.SelectedItem;
            if (selectedKhoi == null)
            {
                monHocComboBox.Items.Clear();
                monHocComboBox.SelectedItem = null;
                monHocComboBox.Text = "";

                lopComboBox.Text = "";
                lopComboBox.SelectedItem = null;
                lopComboBox.Items.Clear();
            }
            else
            {
                this.LoadMonHoc(selectedKhoi, monHocComboBox);

                var result = this._bsServices.GetListOfLopHoc(selectedKhoi.Key).Select(x => new ComboBoxModel
                {
                    Key = x.LopHocId,
                    Value = x.TenLop
                }).ToList();
                lopComboBox.Text = "";
                lopComboBox.SelectedItem = null;
                lopComboBox.Items.Clear();
                foreach (var comboBoxModel in result)
                {
                    lopComboBox.Items.Add(comboBoxModel);
                }
            }
        }
    }
}
